if(typeof coos == "undefined"){var coos = function(){};}

var selectedIndex = -1;
var intTmp;
coos.city = function(){};
coos.city.onkeyup = function(obj,showDiv,cityMap,type,event)
{
	var obj = obj;
	var showDiv = coos.$obj(showDiv);
	var e = coos.event.format(event);
	var keyCode = e.keyCode;
	var isUp = false;
	showDiv.onmouseover = function(){coos.city.timeout = null;};
	if(keyCode==40 || keyCode==38)
	{
		if(keyCode==40){isUp = true;}
		coos.city.chageSelection(obj,showDiv,cityMap,type,e,isUp);
	}
	else if(keyCode == 13)
	{
		coos.city.outSelection(obj,showDiv,cityMap,type,e,selectedIndex);
	}
	else
	{
		coos.city.show(obj,showDiv,cityMap,type,e);
	}
};
coos.city.timeout = null;
coos.city.onblur = function(showDiv)
{
	var showDiv = coos.$obj(showDiv);
	coos.city.timeout = setTimeout(function(){coos.city.close(showDiv);},300);
};
coos.city.show = function(obj,showDiv,cityMap,type,event)
{
	var obj = obj;
	var showDiv = coos.$obj(showDiv);
	var e = coos.event.format(event);
	var value = obj.value;
	if(value.trim() == "")
	{
		return;
	}
	selectedIndex = -1;
	showDiv.innerHTML = "";
	var key = obj.value.trim();
	//匹配以key开头的字符
	var re = "^(" + key + ")+";
	//只匹配一次，不区分大小写
	var oReg = new RegExp(re, "i");
	for(var i=0; i < cityMap.size(); i++)
	{
		//cityMap中value以:广州,guangzhou的形式保存
		var values = cityMap.getValue(i).split(",");
		if(oReg.test(values[0]) || oReg.test(values[1]))
		{
			coos.city.addOption(obj,showDiv,cityMap.getValue(i),cityMap.getKey(i),type);
		}
	}
	
	showDiv.innerHTML += "<div class=close onclick=coos.city.close(\"" + showDiv.id + "\")>关闭&nbsp;</div>";
	coos.city.divPosition(obj,showDiv);
	showDiv.style.display = "";
};
coos.city.chageSelection = function(obj,showDiv,cityMap,type,event,isUp)
{
	if(isUp)
	{
		selectedIndex++;
	}
	else
	{
		selectedIndex--;
	}
	var divs = coos.$tag("div",showDiv);
	var maxIndex = divs.length - 2;
	if(selectedIndex < 0){selectedIndex = 0;}
	if(selectedIndex > maxIndex){selectedIndex = maxIndex;}
	for(intTmp = 0; intTmp <= maxIndex; intTmp++)
	{
		if(intTmp == selectedIndex)
		{
			divs[intTmp].className = "selected";
			obj.value = divs[intTmp].getAttribute("cityname");
			obj.cityname = divs[intTmp].getAttribute("cityname");
			obj.citycode = divs[intTmp].getAttribute("citycode");
		}
		else
		{
			divs[intTmp].className = "content";
		}
	}
};
coos.city.outSelection = function(obj,showDiv,cityMap,type,event,Index)
{
	var divs = coos.$tag("div",showDiv);
	var maxIndex = divs.length - 2;
	if(maxIndex >= Index)
	{
		if(type == "province")
		{
			changeProviceAction(obj,divs[Index].getAttribute("citycode"),divs[Index].getAttribute("cityname"));
		}
		else if(type == "caton")
		{
			changeCatonAction(obj,divs[Index].getAttribute("citycode"),divs[Index].getAttribute("cityname"));
		}
	}
	showDiv.innerHTML = "";
	showDiv.style.display = "none";
};
coos.city.close = function(showDiv)
{
	var showDiv = coos.$obj(showDiv);
	showDiv.innerHTML = "";
	showDiv.style.display = "none";
};
coos.city.divPosition = function(obj,showDiv)
{
	var obj = obj;
	var inputObj = obj;
	var showDiv = showDiv;
	if(coos.browser.msie)
	{
		var top = 0;
		var left = -2;
	}
	else
	{
		var top = 2;
		var left = 0;
	}
	while(obj.offsetParent)
	{
		left += obj.offsetLeft + (obj.currentStyle?(parseInt(obj.currentStyle.borderLeftWidth)).NaN0():0);
		top += obj.offsetTop + (obj.currentStyle?(parseInt(obj.currentStyle.borderTopWidth)).NaN0():0);
		obj = obj.offsetParent;
	}
	left += obj.offsetLeft + (obj.currentStyle?(parseInt(obj.currentStyle.borderLeftWidth)).NaN0():0);
	top += obj.offsetTop + (obj.currentStyle?(parseInt(obj.currentStyle.borderTopWidth)).NaN0():0);
	showDiv.style.top = (top + inputObj.clientHeight) + "px";
	showDiv.style.left = left + "px";
	showDiv.style.width = inputObj.clientWidth + 1 + "px";
};
coos.city.addOption = function(obj,showDiv,value,key,type)
{
	var cityname = value.split(",")[0];
	var citycode = key;
	var text =  cityname + "(" + value.split(",")[1] + ")";
	if(type == "province")
	{
		var html = "<div class=\"content\" citycode=\"" + citycode + "\" cityname=\"" + cityname + "\" onmouseover='this.className=\"selected\";' onmouseout='this.className=\"content\";' onmousedown='changeProviceAction(\"cityInput\",\"" + citycode + "\",\"" + cityname + "\");'>" + text + "</div>";
		showDiv.innerHTML += html;
	}
	else if(type == "caton")
	{
		var html = "<div class=\"content\" citycode=\"" + citycode + "\" cityname=\"" + cityname + "\" onmouseover='this.className=\"selected\";' onmouseout='this.className=\"content\";' onmousedown='changeCatonAction(\"catonInput\",\"" + citycode + "\",\"" + cityname + "\");'>" + cityname + "</div>";
		showDiv.innerHTML += html;
	}
};

//以下为实际应用代码
coos.onloadEvent(function(){
	coos.ajax.get("GetDataService?method=getProvinceByCountry&countryID=86",getProvinceCallback);
});
var citydata = null;
var catondata = null;
var citydataMap = null;
var catondataMap = null;
function getProvinceCallback(responseText)
{
	citydata = responseText.toJson();
	citydataMap = coos.hashmap();
	var getCatonURL = "GetDataService?method=getCaton&code=" + citydata[0].ID + "&type=" + citydata[0].children;
	coos.ajax.get(getCatonURL,getCatonCallback);
	//responseText = [{ID:'8601',name:'安徽',children:'city'},{ID:'8602',name:'北京',children:'city'},{ID:'8603',name:'福建',children:'city'},{ID:'8604',name:'甘肃',children:'city'},{ID:'8605',name:'广东',children:'city'},{ID:'8606',name:'广西',children:'city'},{ID:'8607',name:'贵州',children:'city'},{ID:'8608',name:'海南',children:'city'},{ID:'8609',name:'河北',children:'city'},{ID:'8610',name:'河南',children:'city'},{ID:'8611',name:'黑龙江',children:'city'},{ID:'8612',name:'湖北',children:'city'},{ID:'8613',name:'湖南',children:'city'},{ID:'8614',name:'吉林',children:'city'},{ID:'8615',name:'江苏',children:'city'},{ID:'8616',name:'江西',children:'city'},{ID:'8617',name:'辽宁',children:'city'},{ID:'8618',name:'内蒙古',children:'city'},{ID:'8619',name:'宁夏',children:'city'},{ID:'8620',name:'青海',children:'city'},{ID:'8621',name:'山东',children:'city'},{ID:'8622',name:'山西',children:'city'},{ID:'8623',name:'陕西',children:'city'},{ID:'8624',name:'上海',children:'city'},{ID:'8625',name:'四川',children:'city'},{ID:'8626',name:'天津',children:'city'},{ID:'8627',name:'西藏',children:'city'},{ID:'8628',name:'新疆',children:'city'},{ID:'8629',name:'云南',children:'city'},{ID:'8630',name:'浙江',children:'city'},{ID:'8631',name:'重庆',children:'city'},{ID:'8632',name:'台湾',children:'city'},{ID:'8633',name:'香港',children:'city'},{ID:'8634',name:'澳门',children:'city'}];
	var html = ""; 
	html += '<div class="selectDiv"><select id="citySelect" class="select" onChange="changeProvince()">';
	for(var i=0; i < citydata.length; i++)
	{
		citydataMap.set(citydata[i].ID,citydata[i].name + "," + getSpellAll(citydata[i].name));
		html += '<option value="'+ citydata[i].ID + ',' + citydata[i].children + '">'+ citydata[i].name + '</option>';
	}
	html += '</select></div>';
	html += '<div class="cityInputDiv">';
	html += '<input autocomplete="off" type="text" id="cityInput" citycode="' + citydata[0].ID  + '" cityname="' + citydata[0].name + '" value="' + citydata[0].name + '" class="input" ';
	html += 'onkeyup="coos.city.onkeyup(this,\'searchCity\',citydataMap,\'province\',event);" onfocus="coos.city.show(this,\'searchCity\',citydataMap,\'province\',event);" onblur="coos.city.onblur(\'searchCity\');" />';
	if(coos.browser.msie && coos.browser.version == 6)
	{
		html += '<iframe src="javascript:void(0)" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" style="position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:-1;"></iframe>';
	}
	html += '</div>';
	coos.$insert("bottom","cityDiv",html);
}
function changeProvince()
{
	coos.$id("subButton").disabled = true; 
	coos.$id("catonSelect").disabled = true;
	coos.$id("catonInput").disabled = true;
	var select = coos.$id("citySelect");
	changeProviceAction("cityInput",select.value.split(",")[0],select.options[select.selectedIndex].text,select.value.split(",")[1])
}
function changeProviceAction(input,citycode,cityname,citytype)
{
	var input = coos.$obj(input);
	input.value = cityname;
	input.cityname = cityname;
	input.citycode = citycode;
	var citytype = citytype || "city";
	var getCatonURL = "GetDataService?method=getCaton&code=" + input.citycode + "&type=" + citytype;
	coos.ajax.get(getCatonURL,getCatonCallback);
	coos.$id("ajax_loader").style.display = "";
}
function getCatonCallback(responseText)
{
	catondata = responseText.toJson();
	catondataMap = coos.hashmap();
	//responseText = [{ID:'860101',name:'安庆',children:''},{ID:'860102',name:'蚌埠',children:''},{ID:'860103',name:'巢湖',children:''},{ID:'860104',name:'池州',children:''},{ID:'860105',name:'滁州',children:''},{ID:'860106',name:'阜阳',children:''},{ID:'860107',name:'合肥',children:''},{ID:'860108',name:'淮北',children:''},{ID:'860109',name:'淮南',children:''},{ID:'860110',name:'黄山',children:''},{ID:'860111',name:'六安',children:''},{ID:'860112',name:'马鞍山',children:''},{ID:'860113',name:'宿州',children:''},{ID:'860114',name:'铜陵',children:''},{ID:'860115',name:'芜湖',children:''},{ID:'860116',name:'宣城',children:''},{ID:'860117',name:'亳州',children:''}];
	var html = ""; 
	html += '<div class="selectDiv"><select id="catonSelect" class="select" onChange="changeCaton()">';
	for(var i=0; i < catondata.length; i++)
	{
		catondataMap.set(catondata[i].ID,catondata[i].name + "," + getSpellAll(catondata[i].name));
		html += '<option value="'+ catondata[i].ID + '">'+ catondata[i].name + '</option>';
	}
	html += '</select></div>';
	html += '<div class="catonInputDiv">';
	html += '<input autocomplete="off" type="text" id="catonInput" citycode="' + catondata[0].ID + '" cityname="' + catondata[0].name + '" value="' + catondata[0].name + '" class="input" ';
	html += 'onkeyup="coos.city.onkeyup(this,\'searchCity\',catondataMap,\'caton\',event);" onfocus="coos.city.show(this,\'searchCity\',catondataMap,\'caton\',event);" onblur="coos.city.onblur(\'searchCity\');" />';	
	if(coos.browser.msie && coos.browser.version == 6)
	{
		html += '<iframe src="javascript:void(0)" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" style="position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:-1;"></iframe>';
	}
	html += '</div>';
	coos.$id("catonDiv").innerHTML = "";
	coos.$insert("bottom","catonDiv",html); 
	
	coos.$id("ajax_loader").style.display = "none";
	coos.$id("subButton").disabled = false;
}
function changeCaton()
{
	var select = coos.$id("catonSelect");
	changeCatonAction("catonInput",select.value,select.options[select.selectedIndex].text);
}
function changeCatonAction(input,citycode,cityname)
{
	var input = coos.$obj(input);
	input.value = cityname;
	input.cityname = cityname;
	input.citycode = citycode;
}
function subSelectCity()
{
	if(coos.$id("catonInput").value != coos.$id("catonInput").cityname)
	{
		alert("请选择城市");
		return false;
	}
	parent.coomix.setLocationSelectCity(coos.$id("catonInput").citycode,coos.$id("catonInput").cityname);
	parent.removeDialogbox();
}